package com.yy.QSManage.api;

import cn.hutool.core.util.StrUtil;
import com.yy.QSManage.common.Result;
import com.yy.QSManage.exception.ServiceException;
import com.yy.QSManage.model.entity.FaSysScheduledTasks;
import com.yy.QSManage.service.FaSysScheduledTasksService;
import com.yy.QSManage.service.other.scheduler.ITaskSchedulerService;
import com.yy.QSManage.service.other.scheduler.TaskScheduler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/FaSysScheduledTasksApi")
@Api(tags = "定时任务")
public class FaSysScheduledTasksApi extends BaseApi<FaSysScheduledTasks>{

    @Resource
    private ITaskSchedulerService schedulerService;
    @Autowired
    private FaSysScheduledTasksService scheduledTasksService;

    @Override
    public Result<Boolean> add(@RequestBody FaSysScheduledTasks query) {
        try {
            // 封装定时任务对象
            TaskScheduler taskScheduler = encapsulation(query);
            schedulerService.save(taskScheduler);
            // 查询是否添加成功
            TaskScheduler taskScheduler1 = schedulerService.selectByName(query.getJobName());
            if(StrUtil.isBlank(taskScheduler1.getDescription())){
                throw new ServiceException("添加定时任务失败!");
            }
        }catch (Exception e) {
            throw new ServiceException(e);
        }
        return super.add(query);
    }

    @Override
    public Result<Boolean> edit(@RequestBody FaSysScheduledTasks query) {
        // 封装定时任务对象
        TaskScheduler taskScheduler = encapsulation(query);
        schedulerService.update(taskScheduler);
        return super.edit(query);
    }

    @Override
    public Result<Boolean> del(@PathVariable String id) {
        FaSysScheduledTasks sysScheduledTasks = scheduledTasksService.getById(id);
        schedulerService.delete(sysScheduledTasks.getJobName());
        return super.del(id);
    }

    @ApiOperation("暂停")
    @ApiImplicitParam(name = "jobName", value = "jobName", required = true, dataType = "String", paramType = "path")
    @GetMapping("/pause/{jobName}")
    public Result<Boolean> pause(@PathVariable String jobName) {
        schedulerService.pause(jobName);
        return Result.success();
    }

    @ApiOperation("继续")
    @ApiImplicitParam(name = "jobName", value = "jobName", required = true, dataType = "String", paramType = "path")
    @GetMapping("/resume/{jobName}")
    public Result<Boolean> resume(@PathVariable String jobName) {
        schedulerService.resume(jobName);
        return Result.success();
    }

    /**
     * 封装定时任务对象
     * @param query
     * @return
     */
    private TaskScheduler encapsulation(FaSysScheduledTasks query){
        // 添加定时任务
        TaskScheduler taskScheduler = new TaskScheduler();
        taskScheduler.setJobName(query.getJobName())
                .setStartTime(query.getStartTime())
                .setEndTime(query.getEndTime())
                .setJobClass(query.getJobClass())
                .setCronExpression(query.getCron())
                .setDescription(query.getDescription())
                .setIsDurable(true)
                .setRequestRecovery(true);
        return taskScheduler;
    }
}

